#include <list>
#include <fstream>
#include <boost/serialization/list.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/circular_buffer.hpp>
#include "AppList.h"

AppList::AppList() :
  _list(new std::list<App*>), _appLeft(NULL), _appMiddle(NULL), _appRight(NULL) {
}

void AppList::addApp(App* app) {
  if (_appRight != NULL) {
    _list->push_back(app);
  }
  _appRight = _appMiddle;
  _appMiddle = _appLeft;
  _appLeft = app;
}

void AppList::removeApp(App* app) {
  _list->remove(app);
  //TODO: remove from the displaying apps _currentDisplayedApps->remove(app);
}

App* AppList::getLeftApp() {
  return _appLeft; // _currentDisplayedApps->at(0);
}

App* AppList::getMiddleApp() {
  return _appMiddle;//_currentDisplayedApps->at(1);
}

App* AppList::getRightApp() {
  return _appRight;//_currentDisplayedApps->at(2);
}

App* AppList::getNextRightApp() {
  return _list->front();
}

App* AppList::getNextLeftApp() {
  return _list->back();
}

App* AppList::swapNext() {
  App* enteringApp = getNextRightApp();
  App* leavingApp = _appLeft;

  _appLeft = _appMiddle;
  _appMiddle = _appRight;
  _appRight = enteringApp;

  _list->pop_front();
  _list->push_back(leavingApp);

  return enteringApp;
}

App* AppList::swapPrec() {
  App* enteringApp = _list->back();
  App* leavingApp = _appRight;

  _appRight = _appMiddle;
  _appMiddle = _appLeft;
  _appLeft = enteringApp;

  _list->pop_back();
  _list->push_front(leavingApp);

  return enteringApp;
}

AppList::~AppList() {
  delete _list;
}

